{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "#数据\n",
    "w=[0.1,0.15,0.2,0.25,0.3,0.35,0.4,0.45,0.5,0.55,0.6,0.65]\n",
    "b=[0.35,0.65]\n",
    "I=[5,10]\n",
    "out=[0.01,0.99]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 激活函数\n",
    "def sigmoid(num):\n",
    "    return 1.0/(1+np.exp(-num))\n",
    "\n",
    "# 模拟bp，传入输入值，w，b，输出值\n",
    "def bp1(I,w,b,o):\n",
    "    #fp\n",
    "    h1=sigmoid(w[0]*I[0]+w[1]*I[1]+b[0])\n",
    "    h2=sigmoid(w[2]*I[0]+w[3]*I[1]+b[0])\n",
    "    h3=sigmoid(w[4]*I[0]+w[5]*I[1]+b[0])\n",
    "    \n",
    "    o1=sigmoid(h1*w[6]+h2*w[8]+h3*w[10]+b[1])\n",
    "    o2=sigmoid(h1*w[7]+h2*w[9]+h3*w[11]+b[1])\n",
    "    \n",
    "    e1=0.5*(o[0]-o1)**2\n",
    "    e2=0.5*(o[1]-o2)**2\n",
    "    \n",
    "    #bp (隐层)\n",
    "    t1=-(o[0]-o1)*o1*(1-o1)\n",
    "    t2=-(o[1]-o2)*o2*(1-o2)\n",
    "    tw7=t1*h1\n",
    "    tw9=t1*h2\n",
    "    tw11=t1*h3\n",
    "    \n",
    "    tw8=t2*h1\n",
    "    tw10=t2*h2\n",
    "    tw12=t2*h3\n",
    "    tb1=t2\n",
    "    # 输入层的求导\n",
    "    tw1=(t1*w[6]+t2*w[6])*h1*(1-h1)*I[0]\n",
    "    tw2=(t1*w[6]+t2*w[6])*h1*(1-h1)*I[1]\n",
    "    tw3=(t1*w[6]+t2*w[6])*h2*(1-h2)*I[0]\n",
    "    tw4=(t1*w[6]+t2*w[6])*h2*(1-h2)*I[1]\n",
    "    tw5=(t1*w[6]+t2*w[6])*h3*(1-h3)*I[0]\n",
    "    tw6=(t1*w[6]+t2*w[6])*h3*(1-h3)*I[1]\n",
    "    #...\n",
    "    \n",
    "    tb0=(t1*w[6]+t2*w[6])*h3*(1-h3)\n",
    "    \n",
    "    # 更新\n",
    "    w[0]=w[0]-0.5*tw1\n",
    "    w[1]=w[1]-0.5*tw2\n",
    "    w[2]=w[2]-0.5*tw3\n",
    "    w[3]=w[3]-0.5*tw4\n",
    "    w[4]=w[4]-0.5*tw5\n",
    "    w[5]=w[5]-0.5*tw6\n",
    "    w[6]=w[6]-0.5*tw7\n",
    "    w[7]=w[7]-0.5*tw8\n",
    "    w[8]=w[8]-0.5*tw9\n",
    "    w[9]=w[9]-0.5*tw10\n",
    "    w[10]=w[10]-0.5*tw11\n",
    "    w[11]=w[11]-0.5*tw12\n",
    "    #......\n",
    "    #b[0]=b[0]-0.5*tb0\n",
    "    #b[1]=b[0]-0.5*tb1\n",
    "    return w,b\n",
    "# 迭代\n",
    "for i in range(1000):\n",
    "    w,b=bp1(I,w,b,out)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.1733113867023276, 0.29662277340465509, 0.23123369963864598, 0.31246739927729184, 0.30945598359431126, 0.36891196718862074, -1.4357030235499257, 0.93977178018941609, -1.5348277175023994, 1.0542436687581405, -1.4652817751364198, 1.1587856105261256]\n",
      "[0.35, 0.65]\n"
     ]
    }
   ],
   "source": [
    "print(w)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
